<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
function base_result($data, $success = true, $error_code = '', $error_msg = '', $page_size = 0, $data_count = 0, $page_index = 0)
{
    $result =   array('success' => $success, 'data' => $data, 'error_code' => $error_code, 'error_msg' => $error_msg);
    if($page_size!=0){
        $page_count = floor($data_count / $page_size);
        if ($data_count % $page_size != 0) {
            $page_count++;
        }
        $result["page"]=array('page_size'=>$page_size,'data_count'=>$data_count,
            'page_index'=>$page_index,'page_count'=>$page_count,'query_time'=>date('Y-m-d H:i:s'));
    }
    return json($result);
}

//https请求（支持GET和POST）
function https_request($url, $data = null)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}

function get_device_info($device_id)
{
    $deviceInfoCacheKey = "device_id_" . $device_id;
    $deviceInfo = \think\Cache::get($deviceInfoCacheKey);
    if (!$deviceInfo) {
        $sql = "SELECT id,device_id,firmware_version FROM  billboard where device_id='" . $device_id . "'";
        $data = \think\Db::query($sql);
        if (sizeof($data) > 0) {
            $deviceInfo = array($data[0]["id"], $data[0]["firmware_version"]);
            \think\Cache::set($deviceInfoCacheKey, $deviceInfo);
        }
    }
    return $deviceInfo;
}

function write_log($content, $type = 1)
{
    $type_name = "info";
    if ($type == 2) {
        $type_name = "error";
    } else if ($type == 3) {
        $type_name = "other";
    }else if($type == 4){
        $type_name = "tiaoshi";
    }
    else if($type == 5){
        $type_name = "restart";
    }
    $t = time();
    $base_path = ROOT_PATH . 'logs/' . $type_name . '/' . date("Y-m", $t) . '/' . date("d", $t) . '/' . date("H", $t);
    mkdirs($base_path);
    $filename = $base_path . '/' . date("i") . ".log";
    $Ts = fopen($filename, "a+");
    $content = "[" . date("Y-m-d H:i:s", $t) . ']：' . $content;
    fputs($Ts, "\r\n" . $content);
    fclose($Ts);
}

function mkdirs($dir, $mode = 511)
{
    if (is_dir($dir) || @mkdir($dir, $mode)) {
        return true;
    }

    if (!mkdirs(dirname($dir), $mode)) {
        return false;
    }

    return @mkdir($dir, $mode);
}